// src/routes/index.tsx
import React, { lazy, Suspense, FC } from "react";
import { useRoutes, Navigate } from "react-router-dom";
import { HomeOutlined ,LikeOutlined} from "@ant-design/icons";
import type { XRoutes } from "./types";

import {
  Layout,
  EmptyLayout,
  // CompLayout
} from "../layouts";
import Loading from "@comps/Loading";

import Translation from "@comps/Translation";

const Login = lazy(() => import("@pages/login"));
const Dashboard = lazy(() => import("@pages/dashboard"));
const NotFound = lazy(() => import("@pages/404"));
//1.1 、引入医院设置和医院列表
const HospitalSet = lazy(() => import("@pages/hospital/hospitalset"))
const HospitalList = lazy(() => import("@pages/hospital/hospitallist"))
const AddOrUpDataHos =lazy(()=>import('@pages/hospital/hospitalset/AddOrUpDataHos'))
const HospitalShow =lazy(()=>import('@pages/hospital/hospitallist/hospitalShow'))
const HosScheduling =lazy(()=>import('@pages/hospital/hospitallist/hosScheduling'))
const load = (Comp: FC) => {
  return (
    // 因为路由懒加载，组件需要一段网络请求时间才能加载并渲染l
    // 在组件还未渲染时，fallback就生效，来渲染一个加载进度条效果
    // 当组件渲染完成时，fallback就失效了
    <Suspense fallback={<Loading />}>
      {/* 所有lazy的组件必须包裹Suspense组件，才能实现功能 */}
      <Comp />
    </Suspense>
  );
};

const routes: XRoutes = [
  {
    path: "/",
    element: <EmptyLayout />,
    children: [
      {
        path: "login",
        element: load(Login),
      },
    ],
  },
  {
    path: "/syt",
    element: <Layout />,
    children: [
      {
        path: "/syt/dashboard",
        meta: { icon: <HomeOutlined />, title: <Translation>route:dashboard</Translation> },
        element: load(Dashboard),
      },
      {
        path: "/syt/hospital",
        meta: { icon: <LikeOutlined />, title: '医院管理'},
        element: <EmptyLayout />,
        children:[
          {
          path: "/syt/hospital/hospitallist",
          meta: { title: '医院列表'},
          element: load(HospitalList),
          },
          {
            path: "/syt/hospital/hospitallist/hospitalShow/:id",
            meta: { title: '医院详情'},
            hidden:true,
            element: load(HospitalShow),
          },
          {
            path: "/syt/hospital/hospitallist/hosScheduling/:hoscode",
            meta: { title: '医院排班'},
            hidden:true,
            element: load(HosScheduling),
          },
          {
          path: "/syt/hospital/hospitalset",
          meta: { title: '医院设置'},
          element: load(HospitalSet),
          },
          {
            path: "/syt/hospital/AddHospital",
            meta: { title: '添加医院'},
            //隐藏这个页面，因为不是显示在医院设置里的，所以不用卸载set的子路由里，可以用编程路由导航过来，
            hidden:true,
            element: load(AddOrUpDataHos),
          },
          {
            path: "/syt/hospital/UpDataHos/:id",
            meta: { title: '修改医院'},
            //隐藏这个页面，因为不是显示在医院设置里的，所以不用卸载set的子路由里，可以用编程路由导航过来，
            hidden:true,
            element: load(AddOrUpDataHos),
          }
        ]
      },
    ],
  },

  {
    path: "/404",
    element: load(NotFound),
  },
  {
    path: "*",
    element: <Navigate to="/404" />,
  },
];

// 渲染路由
// 注意：首字母必须大写
export const RenderRoutes = () => {
  // react-router-dom的新增语法。不用自己遍历了，它帮我们遍历生成
  return useRoutes(routes);
};

// 找到要渲染成左侧菜单的路由
export const findSideBarRoutes = () => {
  const currentIndex = routes.findIndex((route) => route.path === "/syt");
  return routes[currentIndex].children as XRoutes;
};

export default routes;
